<!--#include file="lib/utf-8.asp"-->
<!--#include file="lib/conn.asp"-->
<!--#include file="lib/json_lastest.asp"-->
<!--#include file="lib/json_util_lastest.asp"-->
<!--#include file="lib/saferequest.asp"-->

<%
Response.ContentType = "application/json"
repeat=0
bStatus="false"
sInfo=""
aData=""
aVocc=""
aPort=""
aCountry=""
aLog=""
json=""

function CheckVesselRepeat(vessel_id,vessel_name,vessel_vocc)
	sql_repeat="select count(vessel_id) as num from vessel where vessel_name='"&vessel_name&"' and vessel_vocc="&vessel_vocc
	if IsNumeric(vessel_id) and Not IsEmpty(vessel_id) then
		sql_repeat=sql_repeat&" and vessel_id<>"&vessel_id
	end if
	set rs_repeat=Server.CreateObject("ADODB.Recordset")
	rs_repeat.open sql_repeat,conn,1,1
	CheckVesselRepeat=rs_repeat("num")
	rs_repeat.close
	set rs_repeat=nothing
end function

function GenerateCurrentData(sql_cur)
	set rs_cur=server.createobject("adodb.recordset")
	rs_cur.open sql_cur,conn,1,1
	if not rs_cur.eof then
		aData=aData&"{"
		aData=aData&"""vessel_id"":"&rs_cur("vessel_id")&","
		aData=aData&"""vessel_vocc"":"&rs_cur("vessel_vocc")&","
		aData=aData&"""vessel_name"":"""&rs_cur("vessel_name")&""","
		aData=aData&"""vessel_registry"":"&rs_cur("vessel_registry")&","
		aData=aData&"""vessel_country"":"&rs_cur("vessel_country")&","
		aData=aData&"""vessel_start"":"&rs_cur("vessel_start")&","
		aData=aData&"""vessel_end"":"&rs_cur("vessel_end")&","
		aData=aData&"""vessel_chinese"":"""&rs_cur("vessel_chinese")&""","
		aData=aData&"""vessel_english"":"""&rs_cur("vessel_english")&""","
		aData=aData&"""vessel_short"":"""&rs_cur("vessel_short")&""","
		aData=aData&"""vessel_memo"":"""&rs_cur("vessel_memo")&""","
		aData=aData&"""vessel_update"":"""&rs_cur("vessel_update")&""","
		aData=aData&"""vessel_user"":"&rs_cur("vessel_user")
		aData=aData&"}"
	end if
	rs_cur.close
	set rs_cur=nothing
end function

function GenerateVoccData()
	sql_vocc="select vocc_id,vocc_name,vocc_chinese,vocc_english,vocc_short,vocc_memo from vocc order by vocc_name"
	set rs_vocc=server.createobject("adodb.recordset")
	rs_vocc.open sql_vocc,conn,1,1
	do while not rs_vocc.eof
		aVocc=aVocc&"{"
		aVocc=aVocc&"""vocc_id"":"&rs_vocc("vocc_id")&","
		aVocc=aVocc&"""vocc_name"":"""&rs_vocc("vocc_name")&""","
		aVocc=aVocc&"""vocc_chinese"":"""&rs_vocc("vocc_chinese")&""","
		aVocc=aVocc&"""vocc_english"":"""&rs_vocc("vocc_english")&""","
		aVocc=aVocc&"""vocc_short"":"""&rs_vocc("vocc_short")&""","
		aVocc=aVocc&"""vocc_memo"":"""&rs_vocc("vocc_memo")&""""
		aVocc=aVocc&"},"
		rs_vocc.movenext
	loop
	rs_vocc.close
	set rs_vocc=nothing
	if Len(aVocc)>0 then aVocc=Left(aVocc,Len(aVocc)-1)
end function

function GeneratePortData1()
	sql_port="select port_id,port_name,port_country,country_name from port inner join country on port.port_country=country.country_id  order by country_name asc,port_name asc"
	set rs_port=server.createobject("adodb.recordset")
	rs_port.open sql_port,conn,1,1
	do while not rs_port.eof
		aPort=aPort&"{"
		aPort=aPort&"""port_id"":"&rs_port("port_id")&","
		aPort=aPort&"""port_name"":"""&rs_port("port_name")&""","
		aPort=aPort&"""port_country"":"&rs_port("port_country")
'		aPort=aPort&"""country_name"":"""&rs_port("country_name")&""","
		aPort=aPort&"},"
		rs_port.movenext
	loop
	rs_port.close
	set rs_port=nothing
	if Len(aPort)>0 then aPort=Left(aPort,Len(aPort)-1)
end function
function GeneratePortData()
	sql_port="select port_id,port_name,port_country from port inner join country on port.port_country=country.country_id  order by country_name asc,port_name asc"
	set rs_port=server.createobject("adodb.recordset")
	rs_port.open sql_port,conn,1,1
	do while not rs_port.eof
		aPort=aPort&"["
		aPort=aPort&rs_port("port_id")&","
		aPort=aPort&""""&rs_port("port_name")&""","
		aPort=aPort&rs_port("port_country")
		aPort=aPort&"],"
		rs_port.movenext
	loop
	rs_port.close
	set rs_port=nothing
	if Len(aPort)>0 then aPort=Left(aPort,Len(aPort)-1)
end function

function GenerateCountryData1()
	sql_country="select country_id,country_name from country order by country_name"
	set rs_country=server.createobject("adodb.recordset")
	rs_country.open sql_country,conn,1,1
	do while not rs_country.eof
		aCountry=aCountry&"{"
		aCountry=aCountry&"""country_id"":"&rs_country("country_id")&","
		aCountry=aCountry&"""country_name"":"""&rs_country("country_name")&""""
		aCountry=aCountry&"},"
		rs_country.movenext
	loop
	rs_country.close
	set rs_country=nothing
	if Len(aCountry)>0 then aCountry=Left(aCountry,Len(aCountry)-1)
end function
function GenerateCountryData()
	sql_country="select country_id,country_name from country order by country_name"
	set rs_country=server.createobject("adodb.recordset")
	rs_country.open sql_country,conn,1,1
	do while not rs_country.eof
		aCountry=aCountry&"["
		aCountry=aCountry&rs_country("country_id")&","
		aCountry=aCountry&""""&rs_country("country_name")&""""
		aCountry=aCountry&"],"
		rs_country.movenext
	loop
	rs_country.close
	set rs_country=nothing
	if Len(aCountry)>0 then aCountry=Left(aCountry,Len(aCountry)-1)
end function


function ExecuteCommand(sql_cmd)
	set cmd=Server.CreateObject("ADODB.Command")
	cmd.ActiveConnection = conn
	cmd.CommandText = sql_cmd
	cmd.Execute
end function


submit=trim(saferequest("submit",0))
vessel_id=trim(saferequest("vessel_id",0))
if submit<>"" then
	'if IsEmpty(vessel_id) or vessel_id="" then vessel_id="null"
	vessel_vocc=saferequest("vocc",1)
	vessel_name=trim(saferequest("vessel",0))
	vessel_registry=saferequest("registry",1)
	vessel_country=saferequest("country",1)
	vessel_start=saferequest("start",1)
	vessel_end=saferequest("end",1)
	vessel_chinese=trim(saferequest("chinese",0))
	vessel_english=trim(saferequest("english",0))
	vessel_short=trim(saferequest("short",0))
	vessel_memo=trim(saferequest("memo",0))
	vessel_user=session("MM_ID")
	'repeat=CheckVesselRepeat(vessel_id,vessel_name,vessel_vocc)
	if vessel_vocc="" then
		sInfo="请选择船公司"
	elseif vessel_name="" then
		sInfo="船舶名空"
	elseif CheckVesselRepeat(vessel_id,vessel_name,vessel_vocc)>0 then
		sInfo="船舶重复"
	else
		if submit="保存" then
			if not CheckPF(1,4) then
				sInfo="没有编辑权限"
			else
				sql_cmd="update vessel set vessel_vocc="&vessel_vocc&",vessel_name='"&vessel_name&"',vessel_registry="&vessel_registry&",vessel_country="&vessel_country&",vessel_start="&vessel_start&",vessel_end="&vessel_end&",vessel_chinese='"&vessel_chinese&"',vessel_english='"&vessel_english&"',vessel_short='"&vessel_short&"',vessel_memo='"&vessel_memo&"',vessel_user="&vessel_user&",vessel_update='"&Now()&"' where vessel_id="&vessel_id
				ExecuteCommand(sql_cmd)
				sql_cur="SELECT * FROM vessel where vessel_user="&session("MM_ID")&" and vessel_id="&vessel_id
				GenerateCurrentData(sql_cur)
				bStatus="true"
			end if
		elseif submit="新增并复制" then
			if not CheckPF(1,3) then
				sInfo="没有添加权限"
			else
				sql_cmd="insert into vessel(vessel_vocc,vessel_name,vessel_registry,vessel_country,vessel_start,vessel_end,vessel_chinese,vessel_english,vessel_short,vessel_memo,vessel_user) values("&vessel_vocc&",'"&vessel_name&"',"&vessel_registry&","&vessel_country&","&vessel_start&","&vessel_end&",'"&vessel_chinese&"','"&vessel_english&"','"&vessel_short&"','"&vessel_memo&"',"&vessel_user&")"
				ExecuteCommand(sql_cmd)
				sql_cur="SELECT top 1 * FROM vessel where vessel_user="&session("MM_ID")&" order by vessel_id desc"
				GenerateCurrentData(sql_cur)
				bStatus="true"
			end if
		end if
	end if
elseif submit="" then
	if not CheckPF(1,2) then
		sInfo="没有查看权限"
	else
		if vessel_id<>"" then
			sql_cur="select * from vessel where vessel_id="&vessel_id
			GenerateCurrentData(sql_cur)
		end if
		GenerateVoccData()
		GeneratePortData()
		GenerateCountryData()
		bStatus="true"
	end if
end if
'response.write sql
'response.end
if bStatus and submit="" then

	sql_log = "SELECT * FROM vessel where vessel_user="&session("MM_ID")&" and DateValue(vessel_update)=date() order by vessel_update desc"
	set rs_log=server.createobject("adodb.recordset")
	rs_log.open sql_log,conn,1,1
	do while not rs_log.eof
		aLog=aLog&"{"
		aLog=aLog&"""vessel_id"":"&rs_log("vessel_id")&","
		aLog=aLog&"""vessel_vocc"":"&rs_log("vessel_vocc")&","
		aLog=aLog&"""vessel_name"":"""&rs_log("vessel_name")&""","
		aLog=aLog&"""vessel_registry"":"&rs_log("vessel_registry")&","
		aLog=aLog&"""vessel_country"":"&rs_log("vessel_country")&","
		aLog=aLog&"""vessel_start"":"&rs_log("vessel_start")&","
		aLog=aLog&"""vessel_end"":"&rs_log("vessel_end")&","
		aLog=aLog&"""vessel_chinese"":"""&rs_log("vessel_chinese")&""","
		aLog=aLog&"""vessel_english"":"""&rs_log("vessel_english")&""","
		aLog=aLog&"""vessel_short"":"""&rs_log("vessel_short")&""","
		aLog=aLog&"""vessel_memo"":"""&rs_log("vessel_memo")&""","
		aLog=aLog&"""vessel_update"":"""&rs_log("vessel_update")&""","
		aLog=aLog&"""vessel_user"":"&rs_log("vessel_user")
		aLog=aLog&"},"
		rs_log.movenext
	loop
	rs_log.close
	set rs_log=nothing
	if Len(aLog)>0 then aLog=Left(aLog,Len(aLog)-1)
end if



json="{""bStatus"":"&bStatus
json=json&",""sInfo"":"""&sInfo&""""
json=json&",""aData"":["&aData&"]"
json=json&",""aVocc"":["&aVocc&"]"
json=json&",""aPort"":["&aPort&"]"
json=json&",""aCountry"":["&aCountry&"]"
json=json&",""aLog"":["&aLog&"]"
json=json&"}"
response.write(json)
conn.close
set conn=nothing
%>
